package com.bingo.controller;

import com.bingo.common.R;
import com.bingo.entity.HisPool;
import com.bingo.entity.dto.req.ReqForm;
import com.bingo.entity.dto.req.ReqSale;
import com.bingo.service.HisPoolService;
import com.bingo.service.SaleService;
import com.bingo.until.IsToday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sale")
public class SaleController {

    private SaleService saleService;
    @Autowired
    public void setSaleService(SaleService saleService) {
        this.saleService = saleService;
    }

    private HisPoolService hisPoolService;
    @Autowired
    public void setHisPoolService(HisPoolService hisPoolService) {
        this.hisPoolService = hisPoolService;
    }

    //新增销售记录
    @PostMapping("/report")
    public R Sale(@RequestBody ReqSale reqSale) {
        if (!IsToday.isToday(reqSale.getDate())){
            HisPool pool = hisPoolService.getPoolByNo(reqSale.getPoolNo());
            if (pool == null || pool.getType() == 0){
                return R.error("不存在该池塘或为空塘!");
            }
        }
        return saleService.add(reqSale.getPoolNo(), reqSale.getSaleType(), reqSale.getQuantity(), reqSale.getWeight(), reqSale.getDate(), reqSale.getUserId());
    }

    //查询销售记录
    @PostMapping
    public R selectEveryDaySale(@RequestBody ReqForm reqForm) {
        return R.success(saleService.selectEveryDaySale(reqForm.getPoolNos(), reqForm.getDate()[0], reqForm.getDate()[1],reqForm.getSaleType()));
    }

    //查询提交记录
    @PostMapping("/record")
    public R feedLossRecord(@RequestBody ReqForm reqForm) {
        return R.success(saleService.selectSaleRecord(reqForm.getPoolNos(), reqForm.getDate()[0], reqForm.getDate()[1], reqForm.getSaleType(),reqForm.getUserId()));
    }

    //查询场销售记录
    @PostMapping("/site")
    public R siteSale(@RequestBody ReqForm reqForm) {
        return R.success(saleService.selectSiteSale(reqForm.getPoolNos(), reqForm.getDate()[0], reqForm.getDate()[1],reqForm.getSaleType()));
    }
}
